package com.example.it.testformartialdesign;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 7/13/2017 3:56 PM.
 * Class Desc:
 * <p>
 * Update
 * Updated by IT on 7/13/2017 3:56 PM
 * Desc:
 */

public class FruitAdapter extends RecyclerView.Adapter<FruitViewHolder> {

    private final int[] fruitImgs;
    private Context _context;
    private final List<Fruit> _fruitList;

    public FruitAdapter(Context context, List<Fruit> fruitList, int[] fruitImgs) {
        _context = context;
        _fruitList = fruitList;

        this.fruitImgs = fruitImgs;

    }


    //在此方法中实例化我们的VieHorder
    @Override
    public FruitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (_context == null) {
            this._context = parent.getContext();
        }
        View view = LayoutInflater.from(this._context).inflate(R.layout.layout_fruit_item, parent, false);


        final FruitViewHolder holder = new FruitViewHolder(view);
        CardView cardView = view.findViewById(R.id.fruitCard);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = _fruitList.get(position);
                int id = fruitImgs[fruit.getImgId()];
                Intent intent = new Intent(_context, FruitActivity.class);
                intent.putExtra(FruitActivity.FRUIT_NAME, fruit.getName());
                intent.putExtra(FruitActivity.FRUIT_IMAGE_ID, id);
                _context.startActivity(intent);
            }
        });


        return holder;
    }

    //在此方法中控制我们的内容
    @Override
    public void onBindViewHolder(FruitViewHolder holder, int position) {
        Fruit fruit = this._fruitList.get(position);
        holder.furitTitle.setText(fruit.getName());
        LogHelper.i(fruit.getName() + " " + fruit.getImgId());
        //使用第三方插件加载图片数据
        Glide.with(this._context).load(this.fruitImgs[fruit.getImgId()]).into(holder.fruitImage);
    }

    @Override
    public int getItemCount() {
        return this._fruitList.size();
    }
}
